package com.abe.libquick.widget;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.abe.libcore.view.common.BaseCustomView;
import com.abe.libquick.R;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

import java.util.ArrayList;
import java.util.List;

public class DeployableMenu extends BaseCustomView implements View.OnClickListener,
        OnItemClickListener {

    private int menuFlag = 0;//菜单标识
    private boolean isSingle = false;//是否设置为单选
    private boolean isHideTitleAndControl = false;//是否隐藏菜单标题栏
    private OnMenuMultiSelectListener mListener;
    private OnMenuSingleSelectListener sListener;
    private List<MenuEntity> entities;
    private BaseQuickAdapter<MenuEntity, BaseViewHolder> adapter;
    private boolean isOpen = false;//标识是否展开菜单
    //默认GridView一行高度为40dp,默认Title高度40dp
    private float rowHeight = 0;
    private float titleHeight = 0;
    private float menuOpenHeight = 0;
    private float menuCloseHeight = 0;
    /************************************/
    private TextView titleView;
    private ImageView arrowImage;
    private LinearLayout titleBaseView;
    private LinearLayout controlBaseView;

    public DeployableMenu(Context context) {
        this(context, null);
    }

    public DeployableMenu(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DeployableMenu(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected int getRootViewId() {
        return R.id.item_view_root;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.view_deployable_menu;
    }

    @Override
    protected void initContent() {
        titleView = findViewById(R.id.menu_title_text);
        arrowImage = findViewById(R.id.menu_arrow_image);
        LinearLayout controlView = findViewById(R.id.menu_control_layout);
        titleBaseView = findViewById(R.id.menu_title_base_layout);
        RecyclerView showItemGridView = findViewById(R.id.menu_item_grid);
        showItemGridView.setLayoutManager(new GridLayoutManager(getContext(), 3));
        controlBaseView = findViewById(R.id.menu_control_base_layout);
        controlView.setOnClickListener(this);
        //** 初始化 */
        entities = new ArrayList<>();
        adapter = new BaseQuickAdapter<MenuEntity, BaseViewHolder>(R.layout.item_deploy_menu, entities) {
            @Override
            protected void convert(@NonNull BaseViewHolder holder, MenuEntity item) {
                holder.setText(R.id.item_name, item.value);
                if (item.select) {
                    holder.setBackgroundResource(R.id.item_layout, R.drawable.deploy_menu_round_rectangle_bg);
                    holder.setTextColor(R.id.item_name, getResources().getColor(R.color.colorTheme));
                    holder.getView(R.id.item_select).setVisibility(View.VISIBLE);
                } else {

                    holder.setBackgroundResource(R.id.item_layout, R.drawable.deploy_menu_round_rectangle_bg_0);
                    holder.setTextColor(R.id.item_name, Color.parseColor("#666666"));
                    holder.getView(R.id.item_select).setVisibility(View.GONE);
                }
            }
        };
        adapter.setOnItemClickListener(this);
        showItemGridView.setAdapter(adapter);
    }

    //展示标题与折叠展开控制
    public void showTitleControlView() {
        isHideTitleAndControl = false;
        titleBaseView.setVisibility(View.VISIBLE);
        resetHeight();
    }

    //隐藏标题与折叠展开控制
    public void hideTitleControlView() {
        isHideTitleAndControl = true;
        titleBaseView.setVisibility(View.GONE);
        resetHeight();
    }

    //重设菜单高度
    private void resetHeight() {
        if (isHideTitleAndControl) {
            titleHeight = 0;
        } else {
            titleHeight = dip2px(context, 40);
        }
        initMenuHeight();
        if (isOpen) {
            openMenu();
        } else {
            closeMenu();
        }
    }

    //设置数据与监听
    public void setMultiSelectMenuItem(boolean[] select, String[] data,
                                       OnMenuMultiSelectListener listener) {
        if (select == null || data == null || listener == null || select.length != data.length) {
            Toast.makeText(context, "调用存在问题", Toast.LENGTH_SHORT).show();
            return;
        }
        isSingle = false;
        this.mListener = listener;
        this.entities.clear();
        for (int i = 0; i < data.length; i++) {
            entities.add(new MenuEntity(data[i], select[i]));
        }
        //** 刷新数据 */
        adapter.notifyDataSetChanged();
        if (isHideTitleAndControl) {
            titleHeight = 0;
        } else {
            titleHeight = dip2px(context, 40);
        }
        //** 一行高度 */
        rowHeight = dip2px(context, 40);
        initMenuHeight();
        closeMenu();
    }

    //设置数据与监听，设置不选中，position为-1
    public void setSingleSelectMenuItem(int position, String[] data, OnMenuSingleSelectListener listener) {
        if (data == null || listener == null || position > data.length - 1) {
            Toast.makeText(context, "调用存在问题", Toast.LENGTH_SHORT).show();
            return;
        }
        isSingle = true;
        this.sListener = listener;
        for (int i = 0; i < data.length; i++) {
            entities.add(new MenuEntity(data[i], i == position));
        }
        //** 刷新数据 */
        adapter.notifyDataSetChanged();
        if (isHideTitleAndControl) {
            titleHeight = 0;
        } else {
            titleHeight = dip2px(context, 40);
        }
        //** 一行高度 */
        rowHeight = dip2px(context, 40);
        initMenuHeight();
        //** 默认显示一行 */
        closeMenu();
        setSingleSelect(position);
    }

    //初始化菜单高度
    private void initMenuHeight() {
        menuOpenHeight = 0;
        menuCloseHeight = 0;
        int itemNum = entities.size();
        if (itemNum <= 0) {
            return;
        }
        int rows = (itemNum % 3 == 0) ? itemNum / 3 : itemNum / 3 + 1;
        float gridHeight = rows * rowHeight + (rows - 1) * dip2px(context, 10);
        menuOpenHeight = gridHeight + titleHeight;
        menuCloseHeight = rowHeight + titleHeight;
    }

    //获取菜单展开状态
    public boolean isMenuOpen() {
        return isOpen;
    }

    @Override
    public void onClick(View v) {
        if (isOpen) {
            closeMenu();
        } else {
            openMenu();
        }
    }

    //折叠菜单
    public void closeMenu() {
        isOpen = false;
        arrowImage.setBackgroundResource(R.drawable.deploy_arrow_down);
        LayoutParams params = (LayoutParams) controlBaseView
                .getLayoutParams();
        params.height = (int) menuCloseHeight;
        controlBaseView.setLayoutParams(params);
    }

    //展开菜单
    public void openMenu() {
        isOpen = true;
        arrowImage.setBackgroundResource(R.drawable.deploy_arrow_up);
        LayoutParams params = (LayoutParams) controlBaseView
                .getLayoutParams();
        params.height = (int) menuOpenHeight;
        controlBaseView.setLayoutParams(params);
    }

    //选中pos
    public void setSingleSelect(int position) {
        for (int i = 0; i < entities.size(); i++) {
            MenuEntity entity = entities.get(i);
            entity.select = i == position;
            entities.set(i, entity);
        }
        //** 刷新数据 */
        adapter.notifyDataSetChanged();
        if (position < entities.size()) {
            sListener.OnMenuSingleSelect(menuFlag, position);
        }
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        if (isSingle) {
            setSingleSelect(position);
        } else {
            MenuEntity entity = entities.get(position);
            entity.select = !entity.select;
            entities.set(position, entity);
            //** 通知UI */
            boolean[] select = new boolean[entities.size()];
            for (int i = 0; i < entities.size(); i++) {
                select[i] = entities.get(i).select;
            }
            mListener.OnMenuMultiSelect(menuFlag, select);
            adapter.notifyDataSetChanged();
        }
    }

    //获取标识
    public int getMenuFlag() {
        return menuFlag;
    }

    //设置标识
    public void setMenuFlag(int menuFlag) {
        this.menuFlag = menuFlag;
    }

    public String getTitle() {
        return this.titleView.getText().toString().trim();
    }

    //设置标题
    public void setTitle(String title) {
        this.titleView.setText(title);
    }

    //是否为单选
    public boolean isSingle() {
        return isSingle;
    }

    //设置选择模式
    public void setSingle(boolean isSingle) {
        this.isSingle = isSingle;
    }

    //多选菜单监听
    public interface OnMenuMultiSelectListener {
        void OnMenuMultiSelect(int menuFlag, boolean[] select);
    }

    //单选菜单监听
    public interface OnMenuSingleSelectListener {
        void OnMenuSingleSelect(int menuFlag, int position);
    }

    class MenuEntity {
        public String value;
        public boolean select;

        public MenuEntity(String value, boolean select) {
            this.value = value;
            this.select = select;
        }
    }
}